package kino.command;

import kino.client.KinoClient;
import kino.common.KinoConstant;
import kino.database.KinoDB;

@KinoCommandEnable
public class TTLCommand extends AbstractKinoCommand{
    @Override
    public String doExecute(KinoClient client, String... argv) {
        KinoDB kinoDB = client.getDb();
        Long expire = kinoDB.getExpire(argv[0]);
        Long curTime = System.currentTimeMillis();
        Object obj = kinoDB.getDict().get(argv[0]);
        // key 不存在时返回 -2
        if (obj == null) {
            return KinoConstant.ttlNotEXISTS;
        }
        // key 存在但是没有过期时间返回 -1
        if (expire == null) {
            return KinoConstant.cnegone;
        }
        // 返回距离过期时间还剩多少秒
        Long diff = expire - curTime;
        return commandEncoder.encode((int)(diff / 1000));
    }

    @Override
    public Integer getArgc() {
        return 1;
    }

    @Override
    public String getCommandName() {
        return "ttl";
    }

    @Override
    public boolean aofFlag() {
        return false;
    }

    @Override
    public boolean hasExtParam() {
        return false;
    }
}
